package com.kvest.ula.android;

import com.kvest.ula.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.AdapterView;
import android.widget.TextView;

public class ColorPickerDialog extends Dialog
{
	private ColorSelectedListener listener;
	
	public ColorPickerDialog(Context context) 
	{
		super(context);
		
		listener = null;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.color_picker);

		TextView text = (TextView)findViewById(android.R.id.title);
    	if (text != null)
    	{
    		text.setTextColor(getContext().getResources().getColor(R.color.title_text_color));
    	}
		
		final GridView gridViewColors = (GridView) findViewById(R.id.gridViewColors);
		gridViewColors.setAdapter(new ColorPickerAdapter(getContext()));
		
		// close the dialog on item click
		gridViewColors.setOnItemClickListener(new AdapterView.OnItemClickListener() 
		{
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
			{
				if (listener != null)
				{
					listener.colorSelected((Integer)gridViewColors.getAdapter().getItem(position));
				}
				ColorPickerDialog.this.dismiss();
			}
		});		
	}

	public void setListener(ColorSelectedListener listener) 
	{
		this.listener = listener;
	}

	public static interface ColorSelectedListener
	{
		public void colorSelected(Integer color);
	}
}
